let globalData = getApp().globalData;
Page({
    timer: null,
    innerAudioContext:null,
    data: {
        underway: false,  //true为正在录制
        dao:10,
        loading:false
    },
    onLoad() {
        this.ctx = wx.createCameraContext();
    },
    onReady(){
        this.innerAudioContext = wx.createInnerAudioContext()
        this.innerAudioContext.src = 'https://jzy.nuoche.la/images/music.mp3'
    },
    startRecord() {
        let _this = this;
        this.ctx.startRecord({
            success: (res) => {
                _this.innerAudioContext.play()
                _this.innerAudioContext.onError((error)=>{
                    console.log(error)
                })
                _this.timer = setInterval(function () {
                    let time =_this.data.dao;
                    if(time>1){
                        _this.setData({
                            dao:time-1,
                            underway:true
                        })
                    }else{
                        clearInterval(_this.timer)
                        _this.innerAudioContext.pause()
                        _this.innerAudioContext.seek()
                        _this.stopRecord();
                        _this.setData({
                            dao:10,
                            underway:false
                        })
                    }
                }, 1000)
                // _this.timer = setTimeout(function () {
                //     if (_this.data.underway) {
                //         _this.stopRecord();
                //         _this.setData({
                //             underway: false
                //         })
                //     }
                // }, 15000)
            },
        })
    },
    stopRecord() {
        // console.log(stopRecord);
        this.setData({
            underway: false,
            loading: true
        })
        this.ctx.stopRecord({
            success: (res) => {
                // src: res.tempThumbPath,
                //     videoSrc: res.tempVideoPath
                console.log(res);
                console.log('录制结束自动跳转下一步');
                this.timer = null;
                let _this = this;
                wx.showLoading({
                    mask:true,
                    title:'上传中...'
                })
                wx.uploadFile({
                    url: globalData.API + 'order/upload',
                    header: {
                        'content-type': 'multipart/form-data'
                    },
                    filePath: res.tempVideoPath,
                    name: 'file',
                    formData: {
                        'file': 'test'
                    },
                    success(res) {
                        let data = JSON.parse(res.data)
                        if (data.status == 1) {
                            wx.setStorageSync('videoSrc', data.img);
                            wx.navigateBack()
                        }
                    },
                    complete(){
                        wx.hideLoading();
                        _this.setData({
                            underway: false,
                            loading: false
                        })
                    },
                    fail(error){
                        wx.showToast({
                            title: '识别出错，请重新录制',
                            icon:"none"
                        })
                    }
                })

            },
        })
    },
    error(e) {
        console.log(e.detail)
    },
    play(){
        this.audioCtx.play()
    },
    pause(){
        this.audioCtx.pause()
    }
})